﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CodeCube.Common;
using CodeCube.Extensions;
using CodeCube.Service.DtoModel;
using CodeCube.Service.Interfaces;
using CodeCube.Service.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace CodeCube.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Produces("application/json")]
    [ApiExplorerSettings(GroupName = "CodeCube")]
    public class ManageController : ControllerBase
    {
        private readonly ISysMenuService _SysMenuService;

        public ManageController (ISysMenuService sysMenuService)
        {
            _SysMenuService = sysMenuService;
        }
        /// <summary>
        /// 获取单层菜单数据
        /// </summary>
        /// <param name="ParentGuid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<SysMenuModel>>> GetSysMenuList(string ParentGuid)
        {
            return await _SysMenuService.GetListAsync(m => m.ParentGuid == ParentGuid, n => n.Sort, DbOrderEnum.Asc);
        }
        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<SysMenuDto>> GetSysMenuTree()
        {
            return await MakeSysMenuTree("root");
        }
        private async Task<List<SysMenuDto>> MakeSysMenuTree(string parentGuid)
        {
            var l = await _SysMenuService.GetListAsync(m => m.ParentGuid == parentGuid, n => n.Sort, DbOrderEnum.Asc);
            var res = new List<SysMenuDto>();
            foreach(var m in l.data)
            {
                var t = new SysMenuDto(m);
                t.Child = await MakeSysMenuTree(t.Guid);
                res.Add(t);
            }
            return res;
        }
    }
}